<template>
  <div style="height: 100%">
    <el-table v-loading="loading" :data="dataSource" border highlight-current-row @row-click="handleRowClick" height="300px">
      <el-table-column prop="resourceName" label="名称" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="storeUrl" label="存储地址" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="resourceSuffix" label="类型" align="center" width="100px">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" placeholder="过滤" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="resourceSize" label="大小" align="center" width="100px">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" placeholder="过滤" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="updateAt" label="更新日期" align="center" width="150px"></el-table-column>
      <el-table-column label="操作" align="center" width="180px">
        <template slot="header" slot-scope="scope" >
          <el-upload
              :action="uploadUrl"
              :headers="headers"
              :on-success="uploadSuccess"
              :show-file-list="false"
          >
            <el-button type="primary" icon="el-icon-upload2">点击上传</el-button>
          </el-upload>
        </template>
        <div slot-scope="scope">
          <template v-if="select">
            <el-button type="success" icon="el-icon-thumb" circle @click="$emit('rowSelect',scope.row)" title="选择"></el-button>
          </template>
          <template v-else>
            <el-button type="success" icon="el-icon-view" @click="perview(scope.row)"></el-button>
            <el-button type="info" icon="el-icon-download" @click="download(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete-solid" @click="remove(scope.row)"></el-button>
          </template>
        </div>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "StoreResourceTable",
  mixins: [IdeaTable],
  props: {
    select: {
      type: Boolean,
      default: false
    },
    config: {
      type: Object,
      default: function () {
        return { storeId: '0' }
      }
    }
  },
  data() {
    return {
      storeTypes: [
        { value: 'LOCAL', label: '本地磁盘' }, { value: 'FTP', label: 'FTP' },
        { value: 'ALI_OSS', label: '阿里云OSS' }, { value: 'MINI', label: 'MINI IO' }
      ]
    }
  },
  computed: {
    uploadUrl () {
      return this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/resource/upload?storeId=' + this.config.storeId)
    },
    headers () {
      return { Authorization: 'Bearer ' + this.$AuthStorage.getAccessToken() }
    },
    perviewUrl () {
      return this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/resource/preview/')
    },
    downLoadUrl () {
      return this.$Util.mergeUrl(process.env.VUE_APP_BASE_API, '/resource/download/')
    }
  },
  created() {
    this.queryUrl = '/resource/queryPage'
    this.rowKey = 'resourceId'
    this.addFilter({ field: 'storeId', op: '=', value: this.config.storeId })
    this.loadSource()
  },
  methods: {
    initEntity(){
      this.entityInfo = { dataState: 0 }
    },
    uploadSuccess (response) {
      this.$resultMsg(this, response)
      if (response.code === 200) {
        this.loadSource()
      }
    },
    perview (row) {
      if (row.storeUrl && (row.storeUrl.includes('http://') || row.storeUrl.includes('https://'))) {
        window.open(row.storeUrl)
      } else if (row.resourceId) {
        window.open(this.perviewUrl + row.resourceId + '?access_token=' + this.$AuthStorage.getAccessToken())
      }
    },
    download (row) {
      if (row.storeUrl && (row.storeUrl.includes('http://') || row.storeUrl.includes('https://'))) {
        window.open(row.storeUrl)
      } else if (row.resourceId) {
        window.open(this.downLoadUrl + row.resourceId + '?access_token=' + this.$AuthStorage.getAccessToken())
      }
    },
    remove (row) {
      if (row.resourceId) {
        this.$remove(this, 'resource/delete/' + row.resourceId)
            .then(res => {
              this.$resultMsg(this, res, '删除失败')
              if (res.code === 200) {
                this.loadSource()
              }
            })
      }
    }
  }
}
</script>

<style scoped>

</style>
